package com.slmxz.tms.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.slmxz.tms.common.annotation.Log;
import com.slmxz.tms.common.annotation.RepeatSubmit;
import com.slmxz.tms.common.enums.LogModuleEnum;
import com.slmxz.tms.common.model.Option;
import com.slmxz.tms.common.model.Query;
import com.slmxz.tms.common.result.PageResult;
import com.slmxz.tms.common.result.Result;
import com.slmxz.tms.common.support.Condition;
import com.slmxz.tms.system.model.form.RoleForm;
import com.slmxz.tms.system.model.vo.MenuVO;
import com.slmxz.tms.system.model.vo.RoleVO;
import com.slmxz.tms.system.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 角色控制层
 *
 * @author albert
 * @date 2024-11-30
 */
@Tag(name = "03.角色接口")
@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @GetMapping("/page")
    @Operation(summary = "分页列表")
    public PageResult<RoleVO> page(Query query, RoleVO role) {
        IPage<RoleVO> result = this.roleService.selectPage(Condition.getPage(query),role);
        return PageResult.success(result);
    }

    @Operation(summary = "角色下拉列表")
    @GetMapping("/options")
    public Result<List<Option<Long>>> listRoleOptions() {
        List<Option<Long>> list = roleService.listRoleOptions();
        return Result.success(list);
    }

    @Operation(summary = "角色下拉列表")
    @GetMapping("/selectOptions")
    public Result<List<Option<Long>>> selectOptions(RoleVO role) {
        List<Option<Long>> list = roleService.selectOptions(role);
        return Result.success(list);
    }

    @Operation(summary = "编辑角色")
    @PostMapping("/submit")
    @Log(value = "编辑角色",module = LogModuleEnum.ROLE)
    @PreAuthorize("@ss.hasPerm('sys:role:edit')")
    @RepeatSubmit
    public Result<?> submitRole(@Valid @RequestBody RoleForm roleForm) throws Exception {
       this.roleService.saveRole(roleForm);
        return Result.success();
    }

    @Operation(summary = "角色表单数据")
    @GetMapping("/{roleId}/form")
    public Result<RoleForm> getRoleForm(
            @Parameter(description = "角色ID") @PathVariable Long roleId
    ) {
        RoleForm roleForm = roleService.getRoleForm(roleId);
        return Result.success(roleForm);
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:role:delete')")
    @Log(value = "删除角色",module = LogModuleEnum.ROLE)
    public Result<?> deleteRoles(
            @Parameter(description = "删除角色，多个以英文逗号(,)拼接") @PathVariable String ids
    ) {
        boolean result = roleService.deleteRoles(ids);
        return Result.result(result);
    }

    @Operation(summary = "修改角色状态")
    @PutMapping(value = "/{roleId}/status")
    public Result<?> updateRoleStatus(
            @Parameter(description = "角色ID") @PathVariable Long roleId,
            @Parameter(description = "状态(1:启用;0:禁用)") @RequestParam Integer status
    ) {
        boolean result = roleService.updateRoleStatus(roleId, status);
        return Result.result(result);
    }

    @Operation(summary = "获取角色的菜单ID集合")
    @GetMapping("/{roleId}/menuIds/{companyId}")
    public Result<List<Long>> getRoleMenuIds(
            @Parameter(description = "角色ID") @PathVariable Long roleId,
            @Parameter(description = "公司ID") @PathVariable Long companyId
    ) {
        List<Long> menuIds = roleService.getRoleMenuIds(roleId, companyId);
        return Result.success(menuIds);
    }


    @Operation(summary = "分配菜单(包括按钮权限)给角色")
    @PostMapping("/assignMenusPerm")
    @PreAuthorize("@ss.hasPerm('sys:role:assignPerm')")
    @Log(value = "分配角色菜单",module = LogModuleEnum.ROLE)
    @RepeatSubmit
    public Result<?> assignMenusPerm(@RequestBody RoleVO role){
        this.roleService.assignMenusPermToRole(role);
        return Result.success();
    }

}
